//
//  shopTableViewCell.m
//  测试
//
//  Created by 狮子座 on 2017/4/20.
//  Copyright © 2017年 森之源. All rights reserved.
//

#import "shopTableViewCell.h"
#import "shopModel.h"
@interface shopTableViewCell()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *shuliang;
@property (weak, nonatomic) IBOutlet UIButton *selButton;
@property (strong,nonatomic)shopModel *model;
@end
@implementation shopTableViewCell

-(void)update:(shopModel *)model atIndexPath:(NSIndexPath *)indexPath
{
    [_selButton setSelected:model.isSel];
    [_shuliang setText:[NSString stringWithFormat:@"%ld",model.much]];
    self.model = model;
}

- (IBAction)onSelButton:(UIButton *)sender {
    
    if([self.delegate respondsToSelector:@selector(didSelectButtonWithCell:)])
    {
        sender.selected =! sender.selected;
        self.model.isSel =sender.selected;
        [self.delegate didSelectButtonWithCell:self];
    }
    if([self.delegate respondsToSelector:@selector(didSelectButtonWithModel:)])
    {
        sender.selected =! sender.selected;
        self.model.isSel =sender.selected;
        [self.delegate didSelectButtonWithModel:self.model];
    }
    
}
- (IBAction)onJianButton:(UIButton *)button {
    shopModel *model = (id)self.model;
    NSInteger m = model.much ;
    if (m==1) {
        [button setEnabled:NO];
//        [self showBarMessage:@"亲,你真的不想买了嘛??"];
//        [self performBlock:^{
//            [button setEnabled:YES];
//        } afterDelay:1.25f];
        return;
    }
    m--;
    m =  MAX(1,m);//MIN(1,m);//MAX(1,m);
    [model setMuch:m];
    
    [self updateMuch];
    if([self.selectTypeDelegate respondsToSelector:@selector(didSelectType)])
    {
        [self.selectTypeDelegate didSelectType];
    }
    
}

- (IBAction)onJiaButton:(UIButton *)button {
  
     shopModel *model = (id)self.model;
    NSInteger m = model.much ;
    if (m==model.muchMAX) {
        [button setEnabled:NO];
        //[self showBarMessage:@"亲,没有那么多货咯!!"];
        
//        [self performBlock:^{
//            [button setEnabled:YES];
//        } afterDelay:1.25f];
        
        return;
    }
    m++;
    // NSLog(@"%d",model.muchMAX>m?m:model.muchMAX);
    m = MAX(1,model.muchMAX>m?m:model.muchMAX);//MAX(1,m);//MIN(m,1);
    
     [model setMuch:m];
    
    [self updateMuch];
    
    
    if([self.selectTypeDelegate respondsToSelector:@selector(didSelectType)])
    {
        [self.selectTypeDelegate didSelectType];
    }
//    if([self.selectTypeDelegate respondsToSelector:@selector(didSelectType:Button:Cell:)])
//    {
//        [self.selectTypeDelegate didSelectType:selectTypeAdd Button:sender Cell:self];
//    }
}
-(void)updateMuch
{
     [_shuliang setText:[NSString stringWithFormat:@"%ld",self.model.much]];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self.model setMuch:[textField.text integerValue]];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
